/**
 * 用户相关API接口
 */

import type { ApiResponse, PageParams, PageResult } from './types/common'
import type {
  EmailBindReqVO,
  MobileBindReqVO,
  PasswordChangeReqVO,
  UserAchievement,
  UserActivity,
  UserFeedback,
  UserFeedbackCreateReqVO,
  UserFollowerListRespVO,
  UserFollowListRespVO,
  UserFollowStats,
  UserLevel,
  UserLoginReqVO,
  UserLoginRespVO,
  UserPreferences,
  UserProfileRespVO,
  UserProfileUpdateReqVO,
  UserRegisterReqVO,
  UserSettingsRespVO,
  UserSettingsUpdateReqVO,
} from './types/user'
import { http } from '@/utils/request'

/**
 * 获取用户信息
 */
export function getUserProfile() {
  return http.get<ApiResponse<UserProfileRespVO>>('/reading/user/profile')
}

/**
 * 更新用户信息
 */
export function updateUserProfile(data: UserProfileUpdateReqVO) {
  return http.put<ApiResponse<boolean>>('/reading/user/profile', data)
}

/**
 * 获取用户设置
 */
export function getUserSettings() {
  return http.get<ApiResponse<UserSettingsRespVO>>('/reading/user/settings')
}

/**
 * 更新用户设置
 */
export function updateUserSettings(data: UserSettingsUpdateReqVO) {
  return http.put<ApiResponse<boolean>>('/reading/user/settings', data)
}

/**
 * 用户登录
 */
export function userLogin(data: UserLoginReqVO) {
  return http.post<ApiResponse<UserLoginRespVO>>('/reading/user/login', data)
}

/**
 * 用户注册
 */
export function userRegister(data: UserRegisterReqVO) {
  return http.post<ApiResponse<boolean>>('/reading/user/register', data)
}

/**
 * 用户登出
 */
export function userLogout() {
  return http.post<ApiResponse<boolean>>('/reading/user/logout')
}

/**
 * 修改密码
 */
export function changePassword(data: PasswordChangeReqVO) {
  return http.put<ApiResponse<boolean>>('/reading/user/password', data)
}

/**
 * 绑定手机号
 */
export function bindMobile(data: MobileBindReqVO) {
  return http.post<ApiResponse<boolean>>('/reading/user/bind-mobile', data)
}

/**
 * 绑定邮箱
 */
export function bindEmail(data: EmailBindReqVO) {
  return http.post<ApiResponse<boolean>>('/reading/user/bind-email', data)
}

/**
 * 发送手机验证码
 */
export function sendMobileCode(mobile: string) {
  return http.post<ApiResponse<boolean>>('/reading/user/send-mobile-code', { mobile })
}

/**
 * 发送邮箱验证码
 */
export function sendEmailCode(email: string) {
  return http.post<ApiResponse<boolean>>('/reading/user/send-email-code', { email })
}

/**
 * 关注用户
 */
export function followUser(userId: number) {
  return http.post<ApiResponse<boolean>>(`/reading/follow/follow/${userId}`)
}

/**
 * 取消关注用户
 */
export function unfollowUser(userId: number) {
  return http.delete<ApiResponse<boolean>>(`/reading/follow/unfollow/${userId}`)
}

/**
 * 获取关注列表
 */
export function getFollowingList(limit?: number) {
  const params = limit ? { limit } : {}
  return http.get<ApiResponse<UserFollowListRespVO[]>>('/reading/follow/following', params)
}

/**
 * 获取粉丝列表
 */
export function getFollowerList(limit?: number) {
  const params = limit ? { limit } : {}
  return http.get<ApiResponse<UserFollowerListRespVO[]>>('/reading/follow/followers', params)
}

/**
 * 获取关注统计
 */
export function getFollowStats() {
  return http.get<ApiResponse<UserFollowStats>>('/reading/follow/stats')
}

/**
 * 检查是否关注某用户
 */
export function checkFollowStatus(userId: number) {
  return http.get<ApiResponse<boolean>>(`/reading/follow/check/${userId}`)
}

/**
 * 获取用户成就列表
 */
export function getUserAchievements() {
  return http.get<ApiResponse<UserAchievement[]>>('/reading/user/achievements')
}

/**
 * 获取用户等级信息
 */
export function getUserLevel() {
  return http.get<ApiResponse<UserLevel>>('/reading/user/level')
}

/**
 * 获取用户活动记录
 */
export function getUserActivities(params: PageParams) {
  return http.get<ApiResponse<PageResult<UserActivity>>>('/reading/user/activities', params)
}

/**
 * 获取用户偏好设置
 */
export function getUserPreferences() {
  return http.get<ApiResponse<UserPreferences>>('/reading/user/preferences')
}

/**
 * 更新用户偏好设置
 */
export function updateUserPreferences(data: UserPreferences) {
  return http.put<ApiResponse<boolean>>('/reading/user/preferences', data)
}

/**
 * 获取用户反馈列表
 */
export function getUserFeedbackList(params: PageParams) {
  return http.get<ApiResponse<PageResult<UserFeedback>>>('/reading/user/feedback', params)
}

/**
 * 提交用户反馈
 */
export function createUserFeedback(data: UserFeedbackCreateReqVO) {
  return http.post<ApiResponse<number>>('/reading/user/feedback', data)
}

/**
 * 删除用户反馈
 */
export function deleteUserFeedback(id: number) {
  return http.delete<ApiResponse<boolean>>(`/reading/user/feedback/${id}`)
}

/**
 * 上传用户头像
 */
export function uploadAvatar(file: File) {
  const formData = new FormData()
  formData.append('file', file)
  return http.post<ApiResponse<string>>('/reading/user/upload-avatar', formData, {
    'Content-Type': 'multipart/form-data',
  })
}

/**
 * 获取用户统计数据
 */
export function getUserStats() {
  return http.get<ApiResponse<any>>('/reading/user/stats')
}

/**
 * 重置用户密码（忘记密码）
 */
export function resetPassword(data: { mobile: string, code: string, newPassword: string }) {
  return http.post<ApiResponse<boolean>>('/reading/user/reset-password', data)
}

/**
 * 注销用户账号
 */
export function deleteUserAccount(data: { password: string, reason?: string }) {
  return http<ApiResponse<boolean>>({
    url: '/reading/user/account',
    method: 'DELETE',
    data,
  })
}

/**
 * 检查用户名是否可用
 */
export function checkUsernameAvailable(username: string) {
  return http.get<ApiResponse<boolean>>('/reading/user/check-username', { username })
}

/**
 * 检查手机号是否已注册
 */
export function checkMobileRegistered(mobile: string) {
  return http.get<ApiResponse<boolean>>('/reading/user/check-mobile', { mobile })
}

/**
 * 检查邮箱是否已注册
 */
export function checkEmailRegistered(email: string) {
  return http.get<ApiResponse<boolean>>('/reading/user/check-email', { email })
}
